<?php

namespace app\course\service;

use app\course\model\CourseModel;


class CourseService
{
    /**
     * course后台的首页数据
     * @param $filter
     * @param bool|false $isPage
     * @return \think\Paginator
     */
    public function adminCourseList($filter, $isPage = false)
    {
        $where = [
            'c.delete_time' => 0,
            'cc.delete_time' => 0,
            'c.shop_id' => $filter['shop_id'],
            'cc.shop_id' => $filter['shop_id']
        ];
        $join = [

        ];
        $category = empty($filter['category']) ? 0 : intval($filter['category']);
        array_push($join, [
            '__COURSE_CATEGORY__ cc ', 'cc.id = c.category_id'
        ]);
        $field = 'c.*,cc.name,cc.delete_time';
        $courseModel = new CourseModel();
        if (!empty($filter['category'])) {
            $where['c.category_id'] = $category;
        }
        if (empty($filter['enabled']) && isset($filter['enabled'])) {
            $where['c.enabled'] = 0;
            $a = 'selected';
        } else if (!empty($filter['enabled'])) {
            if ($filter['enabled'] == 1) {
                $where['c.enabled'] = 1;
                $b = 'selected';
            }
        }
        if (!empty($filter['keyword'])) {
            $where['c.course_name'] = ['like', "%" . trim($filter['keyword']) . "%"];
        }
        $course = $courseModel->alias('c')->field($field)
            ->join($join)
            ->where($where)
            ->order('update_time', 'DESC')
            ->order('id', 'DESC')
            ->paginate(10, false, ['query' => $filter]);

        return $course;

    }

    /**
     * 关联查询课程对应的课程表
     * @return array
     */
    public function categroy()
    {
        $where = [
            'c.delete_time' => 0,
            'cc.delete_time' => 0,
        ];
        $join = ['__COURSE_CATEGORY__ cc ', 'cc.id = c.category_id'];
        $course_model = new CourseModel();
        $field = 'c.*';
        $data = $course_model->alias('c')->field($field)
            ->join($join)
            ->where($where)
            ->select()
            ->toArray();
        return $data;
    }

}